﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace PVFSLEAP
{
	class C_RPBS
	{
		object PN;

		int hpct = 1;

		float X1;
		float X2;
		float Y1;
		float Y2;

		SortedList STMP = new SortedList();

		public C_RPBS()
		{
			this.X1 = -1;
			this.X2 = -1;
			this.Y1 = -1;
			this.Y2 = -1;
		}

		public void SetLocation( float x1, float x2, float y1, float y2 )
		{
			this.X1 = x1;
			this.X2 = x2;
			this.Y1 = y1;
			this.Y2 = y2;
		}

		public void SetPanel( object pn )
		{
			this.PN = pn;
		}

		public void MeasureCost( C_NODE[] ND )
		{
			for ( int i = 1; i < C_COMM.TTND; i++ )
				ND[i].COST = ( float )Math.Sqrt( ( ( ND[0].POSX - ND[i].POSX ) * ( ND[0].POSX - ND[i].POSX ) ) + ( ( ND[0].POSY - ND[i].POSY ) * ( ND[0].POSY - ND[i].POSY ) ) );
		}

		public void DrawRoutingPath( C_NODE[] ND )
		{
			for ( int p = 1; p < C_COMM.TTND; p++ )
			{
				// Set Child nodes of BS within one-hop
				if ( ND[p].COST <= 100 )
				{
					ND[p].PRID = 0;
					ND[p].HPCT = 1;
					ND[0].CHLD++;
					DrawChildNode( ND[p].POSX, ND[0].POSX, ND[p].POSY, ND[0].POSY );
				}
			}

			for ( int p = 1; p < C_COMM.TTND; p++ )
			{
				int c = -1;

				for ( int t = 1; t < C_COMM.TTND; t++)
				{
					if ( p == t )
						continue;

					SetLocation( ND[p].POSX, ND[t].POSX, ND[p].POSY, ND[t].POSY );
					float dst = MeasureDistance();

					if ( ( dst <= 150 ) && ( ND[p].COST < ND[t].COST ) )
					{
						if ( ND[t].PRID == -1 )
							STMP.Add( t, ND[t].COST.ToString() );

						if ( ( c = MiniumNodeCost() ) != -1 )
						{
							ND[c].PRID = ND[p].NDID;
							DrawChildNode( ND[p].POSX, ND[c].POSX, ND[p].POSY, ND[c].POSY );
						}
					}
					
					STMP.Clear();
				}
			}
		}

		public int FindChildNode( C_NODE[] ND  )
		{
			for ( int i = 1; i < C_COMM.TTND; i++ )
			{
				if ( ( ND[i].HPCT == hpct ) && ( ND[i].CHLD == 0 ) )
					return i;
			}
			hpct++;
			return 0;
		}

		public bool CheckParent( C_NODE[] ND )
		{
			for ( int i = 1; i < C_COMM.TTND; i++ )
			{
				if ( ND[i].PRID == -1 )
					return true;
			}
			return false;
		}

		public int MiniumNodeCost()
		{
			int		scnt = STMP.Count;
			float	cmin = 999999f;
			int		ndid = -1;

			if ( scnt > 0 )
			{
				for ( int i = 0; i < STMP.Count; i++ )
				{
					string	scst = STMP.GetByIndex( i ).ToString();
					float	icst = Convert.ToSingle( scst );

					if ( cmin > icst )
					{
						cmin = icst;
						ndid = Convert.ToInt32( STMP.GetKey( i ) );
					}
				}

				return ndid;
			}
			return -1;
		}


		public float MeasureDistance()
		{
			return ( float )Math.Sqrt( ( ( X1 - X2 ) * ( X1 - X2 ) ) + ( ( Y1 - Y2 ) * ( Y1 - Y2 ) ) );
		}

		public void DrawChildNode( float x1, float x2, float y1, float y2 )
		{
			Pen P		= new Pen( Color.DimGray, 1 );
			P.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
			Graphics G	= ( ( Panel )PN ).CreateGraphics();

			G.DrawLine( P, x1, y1, x2, y2 );
		}

		public int MeasureHopCount( int tgnd, C_NODE[] SN )
		{
			int mvnd = -1;
			int hpct = 0;

			while ( true )
			{
				mvnd = SN[tgnd].PRID;
				hpct++;
				tgnd = SN[mvnd].NDID;

				if ( SN[tgnd].PRID == -1 )
					break;
			}

			return hpct;
		}

	}
}
